package com.mj.haier.calc.servlet.impl;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mj.haier.calc.client.RrskxClient;
import com.mj.haier.calc.servlet.Filter;
import com.mj.haier.calc.servlet.FilterChain;
import com.mj.haier.calc.servlet.Request;
import com.mj.haier.calc.servlet.Response;
import com.mj.haier.calc.servlet.ServletException;
import com.mj.haier.calc.domain.AzEorderSettlementProcess;
import com.mj.haier.calc.domain.BdSwitcher;
import com.mj.haier.calc.domain.CarsInfo;

public class CarsFilter implements Filter {

    private final Logger log = LoggerFactory.getLogger(CarsFilter.class);
    private final RrskxClient rrskxClient;

    public CarsFilter(RrskxClient rrskxClient){
        this.rrskxClient = rrskxClient;
    }


    @Override
    public void doFilter(Request req, Response res, FilterChain chain) throws IOException, ServletException {
        
        AzEorderSettlementProcess process = (AzEorderSettlementProcess)req.getObject();
        log.debug("CarsFilter 过滤前 AzEorderSettlementProcess: {}", process.toString());

        if("0".equals(process.getCarsCompliance())){
            chain.doFilter(req, res);
            return;
        }
        
        CarsInfo carsInfo = rrskxClient.getCarsInfo(process.getNetEmployeeInfoId1());
        BdSwitcher bdSwitcher = rrskxClient.getBdSwitcher(process.getTradeCode());

        process.carsId(carsInfo.getCarsId())
            .carsNumber(carsInfo.getCarsNumber())
            .driverTel(carsInfo.getDriverTel())
            .carsInfoCarsPercent(carsInfo.getCarsPercent())
            .carsInfoCarsTeamPercent(carsInfo.getCarsTeamPercent())
            .add1(bdSwitcher.getSwitcherName());

        //验证合规性
        //车辆编码，合规性必须
        String compliance = "0";
        StringBuilder complianceSb = new StringBuilder();
        if(StringUtils.isBlank(process.getCarsId())){
            compliance = "1";
            complianceSb.append("车辆编码必须, 但不合规").append("&");
        }
   
        if("0".equals(compliance)){
            complianceSb.append("车辆信息合规").append("&");
        }

        process.carsCompliance(compliance).carsMsg(complianceSb.toString());
        log.debug("CarsFilter 过滤后 AzEorderSettlementProcess: {}", process.toString());
        chain.doFilter(req, res);
    }
}